package com.example.yls.demoa;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by yls on 2019/5/15.
 */

public class MyView extends View {
    private Paint paint=new Paint();
    private Path path=new Path();
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.STROKE);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(path,paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
               path.moveTo(event.getX(),event.getY());
               break;
               case MotionEvent.ACTION_MOVE:
                   path.lineTo(event.getX(),event.getY());
                   invalidate();
                   break;
                      case MotionEvent.ACTION_UP:
                         break;
        }

        return true;
    }

    public void setPaintColor(int paintColor) {
        paint.setColor(paintColor);
    }
}
